<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * magunkrol CRUD model class 
 *
 * @author Gabor Bota <gab.bota at 3pixel.hu>
 */
Class Magunkrol_model extends CI_Model {

    public $magunkrol_table;

    function __construct() {
        // Call the Model constructor
        parent::__construct();
        $this->magunkrol_table = $this->db->dbprefix('magunkrol');
    }

    /**
     * 
     * 
     * 
     * Update magunkrol by id.
     * 
     * 
     * @return true on success
     */
    function update_magunkrol($magunkrol_data = array()) {
        // if magunkrol does not exists throw magunkrol_not_exists_exception
        if ($this->get_magunkrol($magunkrol_data['id']) == FALSE)
            throw new not_exists_exception("magunkrol");

        // do selection and update data
        $this->db->where('id', $magunkrol_data['id']);
        $this->db->update($this->magunkrol_table, $magunkrol_data);
    }

    /**
     * 
     * 
     * 
     * Delete magunkrol
     * 
     * 
     * 
     * @param type $id
     */
    function delete($id) {
        // delete magunkrol data from magunkrol_table
        $this->db->where('id', $id);
        $this->db->delete($this->magunkrol_table);
    }

    /**
     * 
     * Add new magunkrol
     * 
     * @param type $magunkrol_data
     */
    function add_magunkrol($magunkrol_data = array()) {
        // insert magunkrol_data
        $this->db->insert($this->magunkrol_table, $magunkrol_data);
    }

    /**
     * 
     * returns the magunkrol (for pagination)
     * 
     * options:
     *  select 
     *  search
     *  order_by
     *  order_dir
     *  limit
     *  offset
     * 
     * @param type $limit the number of returned magunkrol
     * @param type $offset from this number.
     * @return type 
     */
    function get_magunkrolok($options = array()) {

        if (isset($options['select'])) {
            $this->db->select($options['select']);
        }

        if (isset($options['search_like'])) {
            $this->db->like('magunkrol_title', $options['search_like']);
            $this->db->or_like('magunkrol_or', $options['search_like']);
        }

        if (isset($options['limit']) && isset($options['offset']))
            $this->db->limit($options['limit'], $options['offset']);

        if (isset($options['order_by'])) {
            if (isset($options['order_dir'])) {
                $this->db->order_by($options['order_by'], $options['order_dir']);
            } else {
                $this->db->order_by($options['order_by']);
            }
        }

        $query = $this->db->get($this->magunkrol_table);


        return $query->result();
    }

    /**
     * 
     * Returns the number of magunkrol
     * 
     * 
     * @return type 
     */
    function count() {
        return $this->db->count_all_results($this->magunkrol_table);
    }

    /**
     * Gives back magunkrol by id 
     * 
     * @param type $id
     * @return array (id, magunkrolname, magunkrol)
     */
    function get_magunkrol($id) {
        // constructing active record query
        $query = $this->db->get_where($this->magunkrol_table, array('id' => $id));
        // do the query
        $result = $query->result();
        if ($result == NULL)
            return NULL;
        return $result[0];
    }

}